﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel.Composition;

namespace Kooboo.Commerce.Repositories
{
    [Export(typeof(IProductType))]
    public partial class ProductType : Entity, IProductType
    {
       
        IQueryable<IProduct> IProductType.Products
        {
            get {
                return this.DataContext.Products
                    .Where(i => i.ProductTypeId == this.Id)
                    .Select(i => i as IProduct);
            }
        }


        List<ICustomField> _CustomFields;
        public List<ICustomField> CustomFields
        {
            get
            {
                if (this._CustomFields == null)
                {
                    this._CustomFields = this.DataContext.ProductAttributes
                        .Where(i => i.EntityType == (int)EntityType.CustomField && i.ProductTypeId == this.Id)
                        .OfType<ICustomField>()
                        .ToList();
                }

                return this._CustomFields;
            }
            set
            {
                this._CustomFields = value;
            }
        }


        List<IVariation> _Variations;
        public List<IVariation> Variations
        {
            get
            {
                if (this._Variations == null)
                {
                    this._Variations = this.DataContext.ProductAttributes
                        .Where(i => i.EntityType == (int)EntityType.Variation && i.ProductTypeId == this.Id)
                        .OfType<IVariation>()
                        .ToList();
                }

                return this._Variations;
            }
            set
            {
                this._Variations = value;
            }
        }
    }
}
